package com.qilu.pe.ui.activity;

import android.os.Bundle;
import android.widget.ImageView;

import com.androidkun.xtablayout.XTabLayout;
import com.qilu.pe.R;
import com.qilu.pe.base.Base2Activity;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.dao.bean.Sick;
import com.qilu.pe.dao.bean.SickDetail;
import com.qilu.pe.dao.http.APIRetrofit;
import com.qilu.pe.support.util.ActiviceCollector;
import com.qilu.pe.support.util.GlideUtil;
import com.qilu.pe.support.util.PeUtil;
import com.qilu.pe.ui.fragment.SickDetai1Fragment;
import com.qilu.pe.ui.fragment.SickDetail2Fragment;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.util.ToastUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

// 疾病详情
public class SickDetailActivity extends Base2Activity {
    private XTabLayout tab_2;
    private ViewPager vp_tab;

    private VpTabAdapter vpTabAdapter;// tab
    private static final String[] HOME_MODELS = {"概述", "检验"};
    private ImageView iv_img;

    private Sick sick;// 传过来的疾病对象
    public SickDetail detail;// 请求得到的疾病详情对象

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_sick_detail;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActiviceCollector.addActivity(this);
        tab_2 = findViewById(R.id.tab_2);
        vp_tab = findViewById(R.id.vp_tab);
        iv_img = findViewById(R.id.iv_img);
        setHeadTitle("疾病详情");

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            sick = (Sick) bundle.getSerializable("sick");
            setHeadTitle(sick.getName());
            GlideUtil.loadImg(PeUtil.getImageUrl(sick.getPicture()), iv_img);
        }


        for (int i = 0; i < 2; i++) {
            tab_2.addTab(tab_2.newTab().setText(HOME_MODELS[i]), i);
        }
        vpTabAdapter = new VpTabAdapter(getSupportFragmentManager());

        hideProgress();
        reqSickDetail();
        showProgress();
    }

    private Disposable reqSickDetail() {
        return APIRetrofit.getLs().reqSickDetail(sick.getId())
                .compose(RxUtil.<BaseResult2<SickDetail>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<SickDetail>>() {
                    @Override
                    public void accept(BaseResult2<SickDetail> result2) throws Exception {
                        hideProgress();
                        if (result2.isSuccess()) {

                        } else {
                            ToastUtils.showShort(result2.getMsg());
                        }
                        detail = result2.getData();
                        if (null != detail) {
                            vp_tab.setAdapter(vpTabAdapter);
                            tab_2.setupWithViewPager(vp_tab);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                    }
                });
    }

    // 疾病详情 tab adapter
    private class VpTabAdapter extends FragmentPagerAdapter {

        public VpTabAdapter(@NonNull FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            if (position == 1) {
                return SickDetail2Fragment.newInstance("2", detail);
            }
            return SickDetai1Fragment.newInstance("1", detail);
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return HOME_MODELS[position];
        }
    }
}